/*
 * Created by JFormDesigner on Wed May 03 17:32:01 CST 2023
 */

package com.atserver.windows;

import javax.swing.event.*;
import com.atserver.vedio.UsbCameraControl;
import com.atserver.vedio.UsbCameraControlImpl;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author chan
 */
public class TestCamera extends JFrame {

    private UsbCameraControl usbCamera = UsbCameraControlImpl.getInstance();

    public TestCamera() {
        initComponents();
    }

    private void startCamera(ActionEvent e) {
        // TODO add your code here

        // 创建后台任务
        SwingWorker<String, Object> task = new SwingWorker<String, Object>() {
            @Override
            protected String doInBackground() throws Exception {
                usbCamera.doStartCamera("./data/output.mp4");
                // 返回计算结果
                return "Hello";
            }
        };

        // 启动任务
        task.execute();
    }

    private void endCamera(ActionEvent e) {
        // TODO add your code here

        // 创建后台任务
        SwingWorker<String, Object> task = new SwingWorker<String, Object>() {
            @Override
            protected String doInBackground() throws Exception {
                usbCamera.endCamera();
                // 返回计算结果
                return "Hello";
            }
        };

        // 启动任务
        task.execute();
    }

    private void toggleButton1StateChanged(ChangeEvent e) {
        // TODO add your code here
        // 获取事件源（即开关按钮本身）
        JToggleButton toggleBtn = (JToggleButton) e.getSource();
        if(toggleBtn.isSelected()){
            usbCamera.doStartCamera("./data/output.mp4");
        }else {
            usbCamera.endCamera();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        button1 = new JButton();
        button2 = new JButton();
        toggleButton1 = new JToggleButton();

        //======== this ========
        setTitle("test camera");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- button1 ----
        button1.setText("start");
        button1.addActionListener(e -> startCamera(e));
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(60, 20), button1.getPreferredSize()));

        //---- button2 ----
        button2.setText("stop");
        button2.addActionListener(e -> endCamera(e));
        contentPane.add(button2);
        button2.setBounds(new Rectangle(new Point(60, 70), button2.getPreferredSize()));

        //---- toggleButton1 ----
        toggleButton1.setText("camera");
        toggleButton1.addChangeListener(e -> toggleButton1StateChanged(e));
        contentPane.add(toggleButton1);
        toggleButton1.setBounds(new Rectangle(new Point(225, 45), toggleButton1.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(360, 160);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JButton button1;
    private JButton button2;
    private JToggleButton toggleButton1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
